<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Installer - Step 5 - Rebuild Skin Caches
*/
class upgrade_cache
{
	var $refresh_after_step = true;
	var $await_input        = false;

	function upgrade_cache( &$installer )
	{
		$this->installer =& $installer;
		$this->ipsclass  =& $installer->ipsclass;
	}

	function run_step()
	{
		ini_set('display_errors',true);
		error_reporting(E_ALL);

		// The last skin updated
		$last = intval( $this->ipsclass->input['last'] );
		$last = !empty($last) ? $last : 0;

		// Load the cache stuffs:
		require_once( ROOT_PATH . 'sources/lib/admin_cache_functions.php' );
		$cache = new admin_cache_functions();
		$cache->ipsclass =& $this->ipsclass;

		// Load the image url setting:
		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'conf_settings', 'where' => 'conf_key=\'ipb_img_url\'' ) );
		$this->ipsclass->DB->simple_exec();
		$data = $this->ipsclass->DB->fetch_row();
		$this->ipsclass->vars['ipb_img_url'] = ( $data['conf_value'] != '' ) ? $data['conf_value'] : $data['conf_default'] ;
		$this->ipsclass->vars['ipb_img_url'] = ($this->ipsclass->vars['ipb_img_url'] == '{blank}' ) ? '' : $this->ipsclass->vars['ipb_img_url'] ;

		// Load skin sets:
		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'skin_sets') );
		$this->ipsclass->DB->simple_order( 'set_skin_set_id ASC' );
		$this->ipsclass->DB->simple_limit( $last, 1 );
		$this->ipsclass->DB->simple_exec();
				
		// Increment $last
		$last++;
		
		if( !$this->ipsclass->DB->get_num_rows() )
		{
			$this->installer->status_text = 'All skins recached, moving on...';
			return;
		}
		else
		{
			$skin = $this->ipsclass->DB->fetch_row();
			
			$this->installer->status_text .= "<strong>Rebuilding cache on skin set: {$skin['set_name']}</strong><br /><br />";
						
			$cache->_rebuild_all_caches( array( $skin['set_skin_set_id'] ) );
			
			$this->installer->refresh_override = 'index.php?upgrade=1&step='.$this->installer->step.'&last='.$last;

			foreach ( $cache->messages as $key => $value )
			{
				$this->installer->status_text .= $value.'<br />';
			}
			
			$this->installer->status_text .= "<br />Moving on to next skin set...";
		}
	}
}

?>